﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using ConfigGen.Core.SettingsLoader;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.SettingsLoader
{
    [TestFixture]
    public class SpreadsheetHeaderProcessorTests
    {
        [Test]
        public void ProcessHeaderRows001_OutOfRangeFirstArg_ThrowsException()
        {
            var itemUnderTest = new SpreadsheetHeaderProcessor();

            var rows = new Queue<object[]>();
            rows.Enqueue(new object[] { "Column0Heading", "Column1Heading" });

            Assert.Throws<ArgumentOutOfRangeException>(() => itemUnderTest.ProcessHeaderRows(-1, rows));
        }

        [Test]
        public void ProcessHeaderRows001_NullArg_ThrowsException()
        {
            var itemUnderTest = new SpreadsheetHeaderProcessor();
            Assert.Throws<ArgumentNullException>(() => itemUnderTest.ProcessHeaderRows(0, null));
        }

        [Test]
        public void ProcessHeaderRows002_TooFewRows_ThrowsException()
        {
            var itemUnderTest = new SpreadsheetHeaderProcessor();
            var rows = new Queue<object[]>();
            Assert.Throws<SpreadsheetHeaderException>(() => itemUnderTest.ProcessHeaderRows(0, rows));
        }

        [Test]
        public void ProcessHeaderRows003_FirstRowEmpty_ThrowsException()
        {
            var itemUnderTest = new SpreadsheetHeaderProcessor();
            var rows = new Queue<object[]>();
            rows.Enqueue(new object[] { string.Empty, null, DBNull.Value });
            rows.Enqueue(new object[] { string.Empty, null, DBNull.Value });
            Assert.Throws<SpreadsheetHeaderException>(() => itemUnderTest.ProcessHeaderRows(0, rows));
        }

        [Test]
        public void ProcessHeaderRows004_SecondRowIsNotEmpty_ThrowsException()
        {
            var itemUnderTest = new SpreadsheetHeaderProcessor();
            var rows = new Queue<object[]>();
            rows.Enqueue(new object[] { "Column0Heading", "Column1Heading" });
            rows.Enqueue(new object[] { "Column0Data", "Column1Data" });
            Assert.Throws<SpreadsheetHeaderException>(() => itemUnderTest.ProcessHeaderRows(0, rows));   
        }

        [Test]
        public void ProcessHeaderRows005_JaggedSpreadsheet_ThrowsException()
        {
            var itemUnderTest = new SpreadsheetHeaderProcessor();
            var rows = new Queue<object[]>();
            rows.Enqueue(new object[] { "Column0Heading", "Column1Heading" });
            rows.Enqueue(new object[] { null, null, null });
            Assert.Throws<SpreadsheetHeaderException>(() => itemUnderTest.ProcessHeaderRows(0, rows));
        }

        [Test]
        public void ProcessHeaderRows006_BasicTest()
        {
            var itemUnderTest = new SpreadsheetHeaderProcessor();
            var rows = new Queue<object[]>();
            rows.Enqueue(new object[] { "Column0Heading", "Column1Heading", "Column2Heading" });
            rows.Enqueue(new object[] { string.Empty, null, DBNull.Value });
            var columnInfos = itemUnderTest.ProcessHeaderRows(0, rows);

            Assert.AreEqual(3, columnInfos.Count, "Incorrect count");

            Assert.AreEqual("Column0Heading", columnInfos[0].ColumnName, "Column0.ColumnName: incorrect");
            Assert.AreEqual(0, columnInfos[0].ColumnOrdinal, "Column0.ColumnOrdinal: incorrect");

            Assert.AreEqual("Column1Heading", columnInfos[1].ColumnName, "Column1.ColumnName: incorrect");
            Assert.AreEqual(1, columnInfos[1].ColumnOrdinal, "Column1.ColumnOrdinal: incorrect");

            Assert.AreEqual("Column2Heading", columnInfos[2].ColumnName, "Column2.ColumnName: incorrect");
            Assert.AreEqual(2, columnInfos[2].ColumnOrdinal, "Column2.ColumnOrdinal: incorrect");
        }

        [Test]
        public void ProcessHeaderRows007_IgnoredColumnsAreSkipped()
        {
            var itemUnderTest = new SpreadsheetHeaderProcessor();
            var rows = new Queue<object[]>();
            rows.Enqueue(new object[] { "Column0Heading", "Column1Heading", "Column2Heading" });
            rows.Enqueue(new object[] { string.Empty, null, DBNull.Value });
            var columnInfos = itemUnderTest.ProcessHeaderRows(2, rows);

            Assert.AreEqual(1, columnInfos.Count, "Incorrect count");

            Assert.AreEqual("Column2Heading", columnInfos[0].ColumnName, "Column2.ColumnName: incorrect");
            Assert.AreEqual(2, columnInfos[0].ColumnOrdinal, "Column2.ColumnOrdinal: incorrect");
        }
    }
}
